package program;

import java.util.*;
import java.sql.Time;

public class Weekday {

	private ArrayList<TTEntry> entries = new ArrayList<TTEntry>();
	private Date day;
	
	public Weekday() {
		day = new Date();
	}
	
	public void createEntry(Train train, Track track, boolean directionAtoB, Date depart){             
            TTEntry entry = new TTEntry(train, track, directionAtoB, depart);
            
            //if method doesn't conflict, add it to the list of entries for the weekday
            if (validateEntry(entry) == true)
            	entries.add(entry);
            else
            	//further implementation to be done in later iterations
            	throw new IllegalStateException("Entry failed to validate. Try new entry");
        }
	
	public boolean remove(TTEntry entry){
            if(entry == null){
               throw new IllegalArgumentException();
            }
            else{
               return entries.remove(entry); 
            }
        }
	
	//validates a potential entry against the current list of entries for the weekday
	//method returns false if entry conflicts, true if doesn't
	private boolean validateEntry(TTEntry y){
		for (int i=0; i<entries.size(); i++){
			TTEntry x = entries.get(i);
			if (x.getTrack() == y.getTrack()){																	//if using the same track
				if (x.getDirection() == y.getDirection()){														//if in same direction
					if (Math.abs(x.getDepartureTime().getTime()-y.getDepartureTime().getTime()) < 1200000){		//if departure times are within 10 minutes
						return false;
					}
				}
				else{																							//otherwise opposite direction
					if (x.getDepartureTime().getTime() == y.getDepartureTime().getTime())						//if depart times are the same
						return false;
					if (x.getDepartureTime().getTime() < y.getDepartureTime().getTime()){						//if x is smaller, must be the earlier entry
						if (y.getDepartureTime().getTime() < x.getArrivalTime().getTime()+300000)				//therefore test if y depart is less than 5 mins later than x arrive
							return false;
					}
					else{																						//otherwise, y is the earlier entry							
						if (x.getDepartureTime().getTime() < y.getArrivalTime().getTime())						//therefore test that x depart is at least 5 mins later than y arrive
							return false;
					}
				}
			}
		}
		return true;
	}
	
	//validates all entries contained in this weekday
	public boolean validateAll(){
		for(int i = 0; i < entries.size(); i++ ){
			TTEntry tempI = entries.get(i);
            for(int j = i + 1; j < entries.size(); j++){
                TTEntry tempJ = entries.get(j);
                if(tempI.getTrack() == tempJ.getTrack()){
                    if(tempI.getDirection() == tempJ.getDirection()){
                        if(tempJ.getDepartureTime().getTime() -  tempI.getDepartureTime().getTime() <= 600000){
                            return false;
                        }
                    }
                    else{
                        Long tempTime;

                        tempTime = tempI.getArrivalTime().getTime();

                        tempTime += 600000;

                        Date newTime = new Date(tempTime);

                        if(tempJ.getDepartureTime().compareTo(tempI.getDepartureTime()) > 0 && tempJ.getDepartureTime().compareTo(newTime) < 0 ){
                              return false;                                    
                        }                      
                    } 
                }
            }
        }
        return true;
    }
    
	
	
	
    public ArrayList<TTEntry> getList(){
        return entries;
    }
    
    public void setDate(Date date){	day = date;	}
    
    public Date getDate(){	return day;	}
}
	
